# Set eval=TRUE if you want this to run!
install.packages("data.table")
install.packages("ggplot2")
library(data.table)
library(ggplot2)
mpg <- unique(as.data.table(mpg))
ggplot(data = <DATA>) + <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
aesggplot(data = <DATA>) +
<GEOM_FUNCTION>(
mapping = aes(<MAPPINGS>),
stat = <STAT>,
position = <POSITION>
) +
<COORDINATE_FUNCTION> +
<FACET_FUNCTION>
ggplot(data = ggplot2::diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
stat = "count", # this is the default, not necessary in reality
position = position_identity() # not necessary in reality
) +
coord_cartesian() + # not necessary in reality
facet_grid() # not necessary in reality
Examples:
ggtitle(...) vs. labs(title = ...)ggplot(data = ...) or geom_*(data = ...)ggplot() + geom_bar(stat = 'count') or ggplot() + stat_count()This can make learning harder and cause frustration. My advice is that you try to learn one way and stick to it.
ggplot(data = mpg) +
geom_point(mapping = aes(x = cty, y = hwy))
ggplot(data = mpg) +
geom_point(mapping = aes(x = cty, y = hwy)) +
theme_minimal()
ggplot(data = mpg) +
geom_point(mapping = aes(x = cty, y = hwy)) +
theme_dark()
ggplot(data = mpg) +
geom_point(mapping = aes(x = cty, y = hwy)) +
theme_light()
ggplot(data = mpg) +
geom_point(mapping = aes(x = cty, y = hwy)) +
theme_gray()
# mpg <- as.data.table(unique(ggplot2::mpg))
mpg[, class := factor(class, levels = c("midsize", "compact", "suv", "2seater", "minivan",
"pickup", "subcompact"))]
ggplot(data = mpg) +
geom_bar(mapping = aes(x = class, fill = class))
ggplot(data = mpg) +
geom_bar(mapping = aes(x = class, fill = class)) +
scale_fill_manual(
values = c("blue", "green", "red", "yellow", "black", "magenta", "white"),
labels = 1:7
)
ggplot(data = mpg) +
geom_bar(mapping = aes(x = class, fill = class)) +
scale_fill_brewer(palette = "YlOrRd")
Find color palettes here for example.
install.packages("patchwork")
##
## The downloaded binary packages are in
## /var/folders/21/6klmfhg17dx9d__zjjpvsn540000gn/T//RtmpTCOh2u/downloaded_packages
library(patchwork)
p1 <- ggplot(mpg) + geom_bar(aes(x = class, fill = class)) + scale_fill_brewer(palette = "YlOrRd") + coord_flip()
p2 <- ggplot(mpg) + geom_bar(aes(x = class, fill = class)) + scale_fill_brewer(palette = "Blues") + coord_flip()
p1 + p2
p3 <- ggplot(mpg) + geom_bar(aes(x = class, fill = class)) + scale_fill_brewer(palette = "Accent")
(p1 | p2) /
p3 + plot_annotation(title = 'Three plots, one title. Wow.')
Find out more about the package here!
Instead of this:
ggplot(mpg) +
geom_bar(aes(x = class, fill = class)) +
scale_color_brewer(palette = 2) +
ggtitle("Nice plot dude!") +
coord_cartesian(ylim = c(0, 30))
You can do this:
setPlotAesthetics <- function(palette, title, ylimmax) {
list(scale_color_brewer(palette = palette),
ggtitle(title),
coord_cartesian(ylim = c(0, ylimmax))
)
}
ggplot(mpg) +
geom_bar(aes(x = class, fill = class)) +
setPlotAesthetics(3, "Not bad either!", 120)
p <- ggplot(mpg, aes(hwy)) +
geom_histogram()
pg <- ggplot_build(p)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
head(pg$data[[1]])
## y count x xmin xmax density ncount ndensity
## 1 5 5 12.13793 11.58621 12.68966 0.020138889 0.10869565 0.10869565
## 2 0 0 13.24138 12.68966 13.79310 0.000000000 0.00000000 0.00000000
## 3 2 2 14.34483 13.79310 14.89655 0.008055556 0.04347826 0.04347826
## 4 16 16 15.44828 14.89655 16.00000 0.064444444 0.34782609 0.34782609
## 5 28 28 16.55172 16.00000 17.10345 0.112777778 0.60869565 0.60869565
## 6 10 10 17.65517 17.10345 18.20690 0.040277778 0.21739130 0.21739130
## flipped_aes PANEL group ymin ymax colour fill size linetype alpha
## 1 FALSE 1 -1 0 5 NA grey35 0.5 1 NA
## 2 FALSE 1 -1 0 0 NA grey35 0.5 1 NA
## 3 FALSE 1 -1 0 2 NA grey35 0.5 1 NA
## 4 FALSE 1 -1 0 16 NA grey35 0.5 1 NA
## 5 FALSE 1 -1 0 28 NA grey35 0.5 1 NA
## 6 FALSE 1 -1 0 10 NA grey35 0.5 1 NA
install.packages("plotly")
##
## The downloaded binary packages are in
## /var/folders/21/6klmfhg17dx9d__zjjpvsn540000gn/T//RtmpTCOh2u/downloaded_packages
library(plotly)
ggplotly(
ggplot(mpg) +
geom_bar(aes(x = class, fill = class))
)
Check out this open source list of extensions!
For inspiration browse the (R graph gallery)[https://www.r-graph-gallery.com/]!
- First person's first plot
- Written feedback of the above plot from the second person
- Amended plot by the second person
- Second person's first plot
- Written feedback of the above plot from the first person
- Amended plot by the first person
And then repeat for a second time! ***
This is not a competition: give both positive and negative feedback. When you give negative feedback make sure that you do it gently. When you recive negative feedback remember, that feedback is a gift and your can grow through it.
I will be giving points based on both the original plot, the feedback and the amended plot. I will consider the two best out of the four plots when scoring.
submit it via email using the following subject: homework-4-dataviz-<one-name>-<the-other-name>
Find the rmarkdown template in the shared folder!